﻿using BLL;
using Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Collections;

namespace Student
{
    public partial class select_Group : Form
    {

        List<StuChoice> gds;
        List<StuChoice> selected_groups = new List<StuChoice>();
        TeaChoice m_t;
        List<Models.Student> all_student;
        List<Color> colors = new List<Color>();
        List<int> spare_idx = new List<int>();
        Hashtable used_idx = new Hashtable();
        int[] unused = new int[10];

        public select_Group()
        {
            InitializeComponent();
            //颜色配置
            colors.Add(Color.Green);
            colors.Add(Color.Red);
            colors.Add(Color.Orange);
            colors.Add(Color.Orchid);
            colors.Add(Color.Purple);
            colors.Add(Color.Pink);
            colors.Add(Color.Red);
            colors.Add(Color.Orchid);
            colors.Add(Color.AliceBlue);
            colors.Add(Color.Blue);
        }
        //显示组的基本信息
        private void student_list_MouseClick(object sender, MouseEventArgs e)
        {
            try
            {
                if (group_list.CurrentCell == null) return;

                int idx = group_list.CurrentCell.RowIndex;
                Group_name.Text = gds[idx].group_name;
                Project_name.Text = gds[idx].project_name;
                Group_info.Text = gds[idx].project_info;

                var g_members = all_student.Where(stu => stu.group_id == gds[idx].Group_id).ToList();
                Group_members.DataSource = g_members;
            }
            catch
            {
                MessageBox.Show("显示错误!");
            }
        }

        //var choice = get_tChoice(m_t, $"c{i}");
        private int get_tChoice(Models.TeaChoice t_Data, string pro_name)
        {
            return (int)(t_Data.GetType().GetProperty(pro_name).GetValue(t_Data,null)??-1);
        }

        private void set_tChoice(Models.TeaChoice t_Data, string pro_name, int g_id)
        {
            t_Data.GetType().GetProperty(pro_name).SetValue(t_Data, g_id);
        }
        ////核心区域
        private void select_student_Load(object sender, EventArgs e)
        {
            try
            {
                BLL.B_StuChoice b_StuChoice = new BLL.B_StuChoice();
                BLL.B_Teacher b_teaChoice = new BLL.B_Teacher();
                gds = b_StuChoice.GetList();
                group_list.DataSource = gds;


                for (int i = 0; i < 9; i++)
                {
                    group_list.Columns[i].Visible = true;
                }

                BLL.B_Student b_student = new BLL.B_Student();
                all_student = b_student.GetList();

                for (int i = 0; i < LogInfo.Sys_Config.tearch_choice; i++)
                {
                    spare_idx.Add(i);
                    unused[i] = 1;
                    selected_groups.Add(new StuChoice());
                }
                selected_Group.DataSource = new BindingList<StuChoice>(selected_groups);
                for (int i = 0; i < 9; i++)
                {
                    selected_Group.Columns[i].Visible = true;
                }


                B_TeaChoice b_T = new B_TeaChoice();
                m_t = b_T.Select(LogInfo.teacher);

                if (m_t == null)
                {
                    m_t = new Models.TeaChoice();
                    return;
                }
                for (int i = 1; i <= LogInfo.Sys_Config.tearch_choice; i++)
                {
                    var choice = get_tChoice(m_t, $"Choice_{i}");
                    if (choice != -1)
                    {
                        var row_idx = gds.FindIndex(item => item == gds.Where(g => g.Group_id == choice).First());
                        selected_groups[i - 1] = gds[row_idx];
                        group_list.Rows[row_idx].DefaultCellStyle.BackColor = colors[i - 1];
                        used_idx.Add(row_idx, i - 1);
                        unused[i - 1] = 0;
                    }
                }
            }
            catch
            {
                MessageBox.Show("未知错误!");
            }
        }

        private void 提交_Click(object sender, EventArgs e)
        {
            try
            {
                if (LogInfo.sys_Configs[0].teacher_deadline < LogInfo.Server_Time)
                {
                    MessageBox.Show("已经超时，不能再修改！");
                    return;
                }


                int tmp_gid;
                for (int i = 0; i < LogInfo.Sys_Config.tearch_choice; i++)
                {
                    tmp_gid = (int)selected_groups[i].Group_id == 0 ? -1 : (int)selected_groups[i].Group_id;
                    set_tChoice(m_t, $"Choice_{i + 1}", tmp_gid);
                }

                m_t.Teacher_id = LogInfo.teacher.teacher_id;
                BLL.B_TeaChoice b_T = new BLL.B_TeaChoice();
                if (b_T.Commit(m_t))
                {
                    MessageBox.Show("ok!");
                }
            }
            catch
            {
                MessageBox.Show("提交失败!");
            }

        }
        //选组信息
        private void group_list_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (group_list.CurrentCell == null) return;

            select_one(group_list.CurrentCell.RowIndex);
        }
        ////小组选择
        private void select_one(int idx)
        {
            try
            {
                int tmp_idx;

                if (used_idx.ContainsKey(idx))
                {
                    tmp_idx = (int)used_idx[idx];
                    selected_groups[tmp_idx] = new StuChoice();
                    used_idx.Remove(idx);
                    unused[tmp_idx] = 1;
                    group_list.Rows[idx].DefaultCellStyle.BackColor = System.Drawing.SystemColors.Window;
                }
                else
                {
                    if (used_idx.Count == LogInfo.Sys_Config.tearch_choice)
                    {
                        MessageBox.Show("不能选更多的组！");
                        return;
                    }

                    for (int i = 0; i < LogInfo.Sys_Config.tearch_choice; i++)
                    {
                        //
                        if (unused[i] == 1)
                        {
                            unused[i] = 0;
                            used_idx.Add(idx, i);
                            selected_groups[i] = gds[idx];
                            group_list.Rows[idx].DefaultCellStyle.BackColor = colors[i];
                            break;
                        }
                    }
                }
            }
            catch
            {
                MessageBox.Show("未知错误!");
            }

            selected_Group.DataSource = new BindingList<StuChoice>(selected_groups);
        }

        public static bool IsIndexInRange<T>(List<T> array, int index)
        {
            return index >= 0 && index < array.Count;
        }
        //寡人的选择
        private void selected_Group_MouseClick(object sender, MouseEventArgs e)
        {
            try
            {
                int idx = selected_Group.CurrentCell.RowIndex;
                if (!IsIndexInRange(selected_groups, idx))
                    return;

                Group_name.Text = selected_groups[idx].group_name;
                Project_name.Text = selected_groups[idx].project_name;
                Group_info.Text = selected_groups[idx].project_info;

                var g_members = all_student.Where(stu => stu.group_id == selected_groups[idx].Group_id).ToList();
                Group_members.DataSource = g_members;
            }
            catch
            {
                MessageBox.Show("选择失败!");
            }
        }

        //显示行号
        private void selected_Group_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            try
            {
                Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y,
                                                    selected_Group.RowHeadersWidth - 4, e.RowBounds.Height);

                TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
                selected_Group.RowHeadersDefaultCellStyle.Font,
                rectangle,
                selected_Group.RowHeadersDefaultCellStyle.ForeColor,
                TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
            }
            catch
            {
                MessageBox.Show("显示错误!");
            }
        }

        private void label7_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void Group_members_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void selected_Group_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void group_list_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
